Extract contructor for Package where only root_package is needed
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Sat, 19 Sep 2015 17:50:14 +0000 (13:50 -0400)
committerCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Sun, 20 Sep 2015 03:19:16 +0000 (23:19 -0400)
There are many places where both source and its root_package() are used,
but in these places, the only reason the source is created is to get to
the root_package.

This just extracts the source creation into a Package constructor for
now, but I think this can be made to not use source at all.

src/cargo/core/package.rs
src/cargo/ops/cargo_clean.rs
src/cargo/ops/cargo_doc.rs
src/cargo/ops/cargo_pkgid.rs
src/cargo/ops/cargo_run.rs
src/cargo/ops/registry.rs

index f71f5025e5d1f45f6ed189e086d6455db319613b..1860257eaf8c70c487a24ab1c90e5b9a64a48133 100644 (file)
@@ -6,9 +6,10 @@ use semver::Version;
 
 use core::{Dependency, Manifest, PackageId, Registry, Target, Summary, Metadata};
 use core::dependency::SerializedDependency;
-use util::{CargoResult, graph};
+use util::{CargoResult, graph, Config};
 use rustc_serialize::{Encoder,Encodable};
 use core::source::Source;
+use sources::PathSource;
 
 /// Informations about a package that is available somewhere in the file system.
 ///
@@ -58,6 +59,12 @@ impl Package {
         }
     }
 
+    pub fn for_path(manifest_path: &Path, config: &Config) -> CargoResult<Package> {
+        let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
+                                                   config));
+        source.root_package()
+    }
+
     pub fn dependencies(&self) -> &[Dependency] { self.manifest.dependencies() }
     pub fn manifest(&self) -> &Manifest { &self.manifest }
     pub fn manifest_path(&self) -> &Path { &self.manifest_path }
index caae5e4f9c73a3d7023feecb78e95f63cae0fcc4..8a28168c9b6df7fbfc3505534f76ee136852111e 100644 (file)
@@ -3,9 +3,8 @@ use std::fs;
 use std::io::prelude::*;
 use std::path::Path;
 
-use core::{PackageSet, Profiles, Profile};
+use core::{Package, PackageSet, Profiles, Profile};
 use core::source::{Source, SourceMap};
-use sources::PathSource;
 use util::{CargoResult, human, ChainError, Config};
 use ops::{self, Layout, Context, BuildConfig, Kind};
 
@@ -17,9 +16,7 @@ pub struct CleanOptions<'a> {
 
 /// Cleans the project from build artifacts.
 pub fn clean(manifest_path: &Path, opts: &CleanOptions) -> CargoResult<()> {
-    let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
-                                            opts.config));
-    let root = try!(src.root_package());
+    let root = try!(Package::for_path(manifest_path, opts.config));
     let target_dir = opts.config.target_dir(&root);
 
     // If we have a spec, then we need to delete some package,s otherwise, just
index 763512caffcd2f7910d891df8c350f106896e806..11e5d5bf7366af5d8c558f32bd0bd224eb86ddef 100644 (file)
@@ -3,9 +3,8 @@ use std::fs;
 use std::path::Path;
 use std::process::Command;
 
-use core::PackageIdSpec;
+use core::{Package, PackageIdSpec};
 use ops;
-use sources::PathSource;
 use util::{CargoResult, human};
 
 pub struct DocOptions<'a> {
@@ -15,9 +14,7 @@ pub struct DocOptions<'a> {
 
 pub fn doc(manifest_path: &Path,
            options: &DocOptions) -> CargoResult<()> {
-    let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
-                                               options.compile_opts.config));
-    let package = try!(source.root_package());
+    let package = try!(Package::for_path(manifest_path, options.compile_opts.config));
 
     let mut lib_names = HashSet::new();
     let mut bin_names = HashSet::new();
index 9e982aa37cf5a55621701fd178a7a9d8a2299ab6..ab5cfe6911ddc6d7a333dbd408003e195df41002 100644 (file)
@@ -1,16 +1,13 @@
 use std::path::Path;
 
 use ops;
-use core::{PackageIdSpec};
-use sources::{PathSource};
+use core::{PackageIdSpec, Package};
 use util::{CargoResult, human, Config};
 
 pub fn pkgid(manifest_path: &Path,
              spec: Option<&str>,
              config: &Config) -> CargoResult<PackageIdSpec> {
-    let mut source = try!(PathSource::for_path(&manifest_path.parent().unwrap(),
-                                               config));
-    let package = try!(source.root_package());
+    let package = try!(Package::for_path(manifest_path, config));
 
     let lockfile = package.root().join("Cargo.lock");
     let source_id = package.package_id().source_id();
index 5db34762376f1655a917e879d3b17de2f4c875aa..da576189a894c8e1375f01af233e3108202ac6ea 100644 (file)
@@ -2,15 +2,13 @@ use std::path::Path;
 
 use ops::{self, ExecEngine, CompileFilter};
 use util::{self, CargoResult, human, process, ProcessError};
-use sources::PathSource;
+use core::Package;
 
 pub fn run(manifest_path: &Path,
            options: &ops::CompileOptions,
            args: &[String]) -> CargoResult<Option<ProcessError>> {
     let config = options.config;
-    let mut src = try!(PathSource::for_path(&manifest_path.parent().unwrap(),
-                                            config));
-    let root = try!(src.root_package());
+    let root = try!(Package::for_path(manifest_path, config));
 
     let mut bins = root.manifest().targets().iter().filter(|a| {
         !a.is_lib() && !a.is_custom_build() && match options.filter {
index a81dda28ce3385ce6ac440b868c75c8d2f786d8e..d526dc7fc366faa8703f16278c1409f7ddfd0c4b 100644 (file)
@@ -15,7 +15,7 @@ use core::{Package, SourceId};
 use core::dependency::Kind;
 use core::manifest::ManifestMetadata;
 use ops;
-use sources::{PathSource, RegistrySource};
+use sources::{RegistrySource};
 use util::config;
 use util::{CargoResult, human, ChainError, ToUrl};
 use util::config::{Config, ConfigValue, Location};
@@ -31,9 +31,7 @@ pub fn publish(manifest_path: &Path,
                token: Option<String>,
                index: Option<String>,
                verify: bool) -> CargoResult<()> {
-    let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
-                                            config));
-    let pkg = try!(src.root_package());
+    let pkg = try!(Package::for_path(&manifest_path, config));
 
     let (mut registry, reg_id) = try!(registry(config, token, index));
     try!(verify_dependencies(&pkg, &reg_id));
@@ -263,9 +261,7 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
         Some(ref name) => name.clone(),
         None => {
             let manifest_path = try!(find_root_manifest_for_cwd(None));
-            let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
-                                                    config));
-            let pkg = try!(src.root_package());
+            let pkg = try!(Package::for_path(&manifest_path, config));
             pkg.name().to_string()
         }
     };
@@ -325,9 +321,7 @@ pub fn yank(config: &Config,
         Some(name) => name,
         None => {
             let manifest_path = try!(find_root_manifest_for_cwd(None));
-            let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
-                                                    config));
-            let pkg = try!(src.root_package());
+            let pkg = try!(Package::for_path(&manifest_path, config));
             pkg.name().to_string()
         }
     };